﻿
using System;
using Gum.FizzBuzz;
using Gum.FizzBuzz.Writers;

namespace Gum.FizzBuzzApp
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintInstructions();

            while(true)
            {
                var start = GetStartOfRange();
                var end = GetEndOfRange();

                RunJob(start, end);

                Console.WriteLine("\nPretty sweet, right?  Wanna do it again??");
                Console.WriteLine("Press Y if you do and any other key to quit.");
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Y)
                {
                    Console.WriteLine("\n");
                    continue;
                }
                Console.WriteLine("\nOkay, well it's been real.  See you later!");
                return;
            }
            
        }

        private static void RunJob(int start, int end)
        {
            Console.WriteLine("\n");
            var job = new FizzBuzzJob(start, end);
            var writer = new ConsoleStringWriter();
            var runner = new FizzBuzzJobRunner();
            runner.Run(job, writer);
        }

        private static int GetStartOfRange()
        {
            Console.WriteLine("Please enter the number to start counting from and press ENTER.");
            return GetNumericInput();
        }

        private static int GetEndOfRange()
        {
            Console.WriteLine("Please enter the number to end counting at and press ENTER.");
            return GetNumericInput();
        }

        private static int GetNumericInput()
        {
            var inputValue = Console.ReadLine();
            int returnValue;
            while (!int.TryParse(inputValue, out returnValue)) {
                PrintParseErrorFeedback();
                inputValue = Console.ReadLine();
            }
            Console.WriteLine();
            return returnValue;
        }

        private static void PrintParseErrorFeedback()
        {
            Console.WriteLine("Hmmm, I can't seem to convert the value you entered into an integer.");
            Console.WriteLine("Try entering a number like -10, 1, 2, 100, etc. and press ENTER.");
        }

        private static void PrintInstructions()
        {
            Console.WriteLine("Welcome to the FizzBuzz Console App.\n");
            Console.WriteLine("Here's how it works.  You will enter a starting number and an ending number.");
            Console.WriteLine("I will count from the starting number to the ending number.  If the number");
            Console.WriteLine("is evenly divisible by 3, I will write 'Fizz'.  If the number is evenly ");
            Console.WriteLine("divisible by 5, I will write 'Buzz'.  If the number is evenly divible by");
            Console.WriteLine("both 3 and 5, I will write 'FizzBuzz'.  Otherwise, I will just write the number.\n");
            Console.WriteLine("Got it?  Okay, then let's get started.\n");
        }
    }
}
